<?php
namespace home\Model;
use Think\Model;
class RoomModel extends Model
{
    public function get_collection_rooms($user_id)
    {
        $rooms = M('collection as a')
            ->join('left join room as b on a.object_id = b.id left join user as c on a.user_id = c.id')
            ->field('a.*,c.nick_name , c.head_ico')
            ->where('a.type = 1 and a.user_id = '.$user_id)
            ->field('b.*')
            ->select();
        return $this->parse_rooms($rooms);
    }

    public function get_my_rooms($user_id){
        $rooms = M('room as a')->join('left join user as b on a.user_id = b.id')
            ->field('a.*,b.nick_name , b.head_ico')
            ->where('a.user_id = '.$user_id)->select();
        return $this->parse_rooms($rooms);
    }

    public function parse_rooms($rooms){
        foreach ($rooms as $key => $value) {
            $rooms[$key] = $this->parse_room($rooms[$key]);
        }
        return $rooms;
    }

    public function collection(){
        $data['type'] = I('post.type');
        $data['user_id'] = I('post.user_id');
        $data['object_id'] = I('post.object_id');
        $where = 'type = '.$data['type'].' and user_id = '.$data['user_id'].' and object_id ='.$data['object_id'];
        $check = M('collection')->where($where)->count();
        if ($check > 0){
            M('collection')->where($where)->delete();
            sus("取消成功");
        }else{
            M('collection')->add($data);
            sus("操作成功");
        }
        
    }

    public function get_room_info($id,$user_id)
    {
        $room = M('room as a')->join('left join user as b on a.user_id = b.id left join type as c on a.type_id = c.id')
            ->field('a.*,b.nick_name , b.head_ico , c.name as type_name')->where('a.id = '.$id)->find();
        return $this->parse_room($room,$user_id);
        
    }



    public function parse_room($room,$user_id=""){
        $room['tags'] = json_decode($room['tags'],true);
        $room['tags2'] = json_decode($room['tags2'],true);
        $room['tags3'] = json_decode($room['tags3'],true);
        $room['create_time_str'] = date('Y-m-d',$room['create_time']);
        $room['collection_number'] = M('collection')->where('type = 1 and object_id = '.$room['id'])->count();
        $room['collection_number'] = friendlyNumber($room['collection_number']);

        $room['content_number'] = M('content')->where('room_id = '.$room['id'])->count();
        $room['content_number'] = friendlyNumber($room['content_number']);


        $room['click_number'] = M('content')->where('room_id = '.$room['id'])->sum("click");
        if ($room['click_number'] == null){
            $room['click_number'] =0;
        }
        $room['click_number'] = friendlyNumber($room['click_number']);
        if ($user_id != "") {
            if ($room['user_id'] == $user_id) {
                $room['is_me'] = 1;
            }else{
                $room['is_me'] = 0;
            }
            $room['is_collection'] =
                M('collection')->where('user_id = '.$user_id.' and object_id = '.$room['id'].' and type = 1')
                ->count();
        }else{
            $room['is_collection'] = 0;
            $room['is_me'] = 0;
        }

        $url = $room['bg_img']."?x-oss-process=image/average-hue";
        //var_dump($url);
        $room['bg_color'] = substr(json_decode(file_get_contents($url),true)['RGB'],2) ;
        //var_dump($room['bg_color']);exit();

        if ($room['status'] == 0){
            $room['status_str'] = "已删除";
        }else if ($room['status'] == 1){
            $room['status_str'] = "待审核";
        }else if ($room['status'] == 2){
            $room['status_str'] = "正常";
        }else if ($room['status'] == 3){
            $room['status_str'] = "未通过审核";
        }
        $room['user'] = D('User')->parse_user( M('user as a')->join('left join city as b on a.city_id = b.id')->where('a.id = '.$room['user_id'])->field('a.* , b.name as city_name')->find() ) ;

        return $room;
    }

}